package cn.schoolwow.ams.module.block.form.api.flow;

import cn.schoolwow.ams.module.block.form.initial.domain.FormBlockContext;
import cn.schoolwow.ams.module.initial.domain.InitialAMSContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetFormBlockContextFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String projectName = (String) flowContext.checkData("projectName");
        String simpleName = (String) flowContext.checkData("simpleName");
        InitialAMSContext initialAMSContext = flowContext.checkInstanceData(InitialAMSContext.class);

        for(FormBlockContext formBlockContext:initialAMSContext.formBlockContextList){
            if(formBlockContext.projectName.equalsIgnoreCase(projectName)
                    &&formBlockContext.formClass.getSimpleName().equalsIgnoreCase(simpleName)
            ){
                flowContext.putTemporaryInstanceData(formBlockContext);
                break;
            }
        }
        if(!flowContext.containInstanceKey(FormBlockContext.class)){
            throw new IllegalArgumentException("查找FormBlockContext上下文失败!项目名:"+projectName+",实体类名:"+simpleName);
        }
    }

    @Override
    public String name() {
        return "设置FormBlock类型上下文数据";
    }
}
